<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/11/15
 * Time: 18:30
 */

namespace app\api\controller;


use app\admin\model\user\UserWithdraw;
use app\common\controller\Api;

class Withdraw extends Api
{
    protected $noNeedLogin = [];

    protected $noNeedRight = ['*'];

    /**
     * 提现列表
     * */
    public function withdraw_list()
    {
        $page = $this->request->param('page', 1);
        $page = ($page * 20) - 20;
        $uw = new UserWithdraw();
        $list = $uw->alias('w')
            ->join('fa_user_card c','c.card_id = w.card_id')
            ->where(['w.uid' => $this->auth->id])
            ->limit($page, 10)
            ->field('w.money,w.status,w.error_ramark,w.create_time,w.audit_time,c.bank_card_number,c.bank_name')
            ->order('w.create_time desc')
            ->select();
        if ($list) {
            $listArr = collection($list)->toArray();
            foreach ($listArr as $key => $value) {
//                $listArr[$key]['status'] = $this->getStatus($value['status']);
                $listArr[$key]['bank_card_number'] = bankCardNumFormat($value['bank_card_number']);
            }
        } else {
            $listArr = [];
        }

        $this->success('请求成功', $listArr, 200);
    }

    public function getStatus($status)
    {
        switch ($status) {
            case "1":
                $statusStr = '提现成功';
                break;
            case "2":
                $statusStr = '拒绝提现';
                break;
            case "0":
                $statusStr = '待审核';
                break;
        }

        return $statusStr;
    }

    /**
     * 申请提现
     * */
    public function add_withdraw()
    {
        $params = $this->request->param();
        $user_money = $this->auth->money;
        if (!isset($params['money']) || empty($params['money'])) {
            $this->error('请输入提现金额');
        }
        $money = (float)$params['money'];
        if ($money > $user_money) {
              $this->error('余额不足');
        }
        $params['uid'] = $this->auth->id;
        $params['before_money'] = $user_money;
        $params['after_money'] = $user_money - $money;
        $params['status']  = 0;
        $params['create_time'] = date('Y-m-d H:i:s',time());
        $uwv = new \app\admin\validate\user\UserWithdraw();
        $userWithdraw = new UserWithdraw();
        if($uwv->check($params)){
            $insertId = $userWithdraw->insert($params);
            if($insertId){
                $this->auth->getUser()->setDec('money',$money);
                $this->success('提现成功,待审核','',200);
            }else{
                $this->error('提现失败，请重试');
            }
        }else{
            $this->error($uwv->getError());
        }
    }
}